import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/Login'
import home from '../components/Home'
import { Message } from 'element-ui'
import homePage from '../views/HomePage'
import user from '../views/personnel/User'
import administration from '../views/personnel/Administrators'
import commodityList from '../views/commodity/CommodityList'
import addCommodity from '../views/commodity/AddCommodity'
import additional from '../views/additional/additional'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: login
  },
  {
    path:'/login',
    name:'login',
    component:login
  },
  {
    path: '/home',
    name: 'home',
    component: home,
    children: [
      {
        path: 'homePage',
        name: 'homePage',
        component: homePage
      },
      {
        path: '/',
        redirect: 'homePage'
      },
      {
        path: 'user',
        name: 'user',
        component: user,
        meta: {
          name: '账户管理',
          isShow: true,
          title: '用户账户'
        }
      },
      {
        path: 'administration',
        name: 'administration',
        component: administration,
        meta: {
          name: '账户管理',
          isShow: true,
          title: '管理员账户'
        }
      },
      {
        path: 'commodityList',
        name: 'commodityList',
        component: commodityList,
        meta: {
          name: '商品管理',
          isShow: true,
          title: '商品信息'
        }
      },
      {
        path: 'addCommodity',
        name: 'addCommodity',
        component: addCommodity,
        meta: {
          name: '商品管理',
          isShow: true,
          title: '添加商品商品'
        }
      },
      {
        path: 'additional',
        name: 'additional',
        component: additional,
      }
    ]
  },
  
]

const router = new VueRouter({
  routes
})

// 全句路由守卫
router.beforeEach((to,from,next) =>{
  console.log('from',from);
  console.log('to',to);
  
  // 放行的路径
  if(from.path == '/login' || to.path == '/'){
    next()
    return
  }

  // 验证token 决定放行
  let token = sessionStorage.getItem('token')
  if(token){
    next()
    Message.success('token认证成功！')
    return 
  }

  Message.error('token认证错误')
})

export default router
